package com.app.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.ValidationException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.app.entity.ResidentialComplaint;
import com.app.entity.ResidentialGuarantee;
import com.app.entity.User;
import com.app.exception.ValidateException;
import com.app.responseVO.PageVO;
import com.app.responseVO.ReturnResultResponseVO;
import com.app.responseVO.UserResponseVO;
import com.app.service.UserComplaintService;
import com.app.service.UserGuaranteeService;

@Controller
@RequestMapping("/userComplaint")
public class UserComplaintController extends BaseController{

	@Autowired
	private UserComplaintService userComplaintService;
	
	@RequestMapping(value = "/insertUserGuarantee")
    public void insertUserComplaint(ResidentialComplaint residentialComplaint,
                           HttpServletResponse response) throws IOException {
        UserResponseVO userResponseVO = new UserResponseVO();
        //ResidentialGuarantee oResidentialGuarantee = JSON.parseObject(body, ResidentialGuarantee.class);
        try {
        	userResponseVO = userComplaintService.insertUserComplaint(residentialComplaint);
        } catch (ValidateException e) {
            userResponseVO.setStatue(false);
            userResponseVO.setMsg("失败");
        }
        writeJSON(response, userResponseVO);
    }
	@RequestMapping(value = "/updateUserComplaint")
	public void updateUserComplaint(ResidentialComplaint oResidentialGuarantee,
			HttpServletResponse response) throws IOException {
		UserResponseVO userResponseVO = new UserResponseVO();
		//ResidentialComplaint oResidentialGuarantee = JSON.parseObject(body, ResidentialComplaint.class);
		try {
			userResponseVO = userComplaintService.updateUserComplaint(oResidentialGuarantee);
		} catch (ValidateException e) {
			userResponseVO.setStatue(false);
			userResponseVO.setMsg("失败");
		}
		writeJSON(response, userResponseVO);
	}
	@RequestMapping(value = "/deleteUserComplaint")
	public void deleteUserComplaint(ResidentialComplaint oResidentialGuarantee,
			HttpServletResponse response) throws IOException {
		UserResponseVO userResponseVO = new UserResponseVO();
		//ResidentialComplaint oResidentialGuarantee = JSON.parseObject(body, ResidentialComplaint.class);
		try {
			userResponseVO = userComplaintService.deleteUserComplaint(oResidentialGuarantee);
		} catch (ValidateException e) {
			userResponseVO.setStatue(false);
			userResponseVO.setMsg("失败");
		}
		writeJSON(response, userResponseVO);
	}
	
	@RequestMapping("/selectUserComplaintList")
    public void selectUserComplaintList(ResidentialComplaint oResidentialGuarantee,@RequestParam("currentPage") int currentPage,@RequestParam("pagesize") int pagesize,
                              HttpServletResponse response) throws IOException {
        ReturnResultResponseVO<ResidentialComplaint> returnResultResponseVO = new ReturnResultResponseVO<ResidentialComplaint>();
        try {
        	//ResidentialComplaint oResidentialGuarantee = new ResidentialComplaint();
        	//oResidentialGuarantee = JSON.parseObject(body, ResidentialComplaint.class);
            returnResultResponseVO = userComplaintService.selectUserComplaintList(oResidentialGuarantee,currentPage, pagesize);
            returnResultResponseVO.setStatue(true);
            
        } catch (ValidationException e) {
            returnResultResponseVO.setStatue(false);
        }
  
        writeJSON(response, returnResultResponseVO);
      
    }
	@RequestMapping("/selectUserComplaintPageList")
	@ResponseBody
	public JSONObject selectUserComplaintPageList(ResidentialComplaint oResidentialGuarantee,@RequestParam("page") int currentPage,@RequestParam("limit") int pagesize,
			HttpServletResponse response,HttpServletRequest request) throws IOException {
		ReturnResultResponseVO<ResidentialComplaint> returnResultResponseVO = new ReturnResultResponseVO<ResidentialComplaint>();
		JSONObject jsonObject = new JSONObject();
		try {
			//ResidentialComplaint oResidentialGuarantee = new ResidentialComplaint();
			//oResidentialGuarantee = JSON.parseObject(body, ResidentialComplaint.class);
			oResidentialGuarantee.setUserName((String)request.getSession().getAttribute("userLogin"));
			returnResultResponseVO = userComplaintService.selectUserComplaintPageList(oResidentialGuarantee,currentPage, pagesize);
			returnResultResponseVO.setStatue(true);
			jsonObject.put("count", returnResultResponseVO.getPage().getPagesize());  
			jsonObject.put("data", returnResultResponseVO.getData());  
		} catch (ValidationException e) {
			returnResultResponseVO.setStatue(false);
		}
		jsonObject.put("code", 0);  
		jsonObject.put("msg", ""); 
		//writeJSON(response, returnResultResponseVO);
		return jsonObject;
	}
	@RequestMapping("/tousuFormPage")
	public String tousuForm(){
		return "tousuForm";
	}
}
